<template>
  <div class="com-container" @dblclick="revertMap">
    <div class="com-chart" ref="map_ref"></div>
  </div>
</template>

<script>
import axios from 'axios'
import { getProvinceMapInfo } from '@/utils/map_utils'
export default {
  data() {
    return {
      chartInstance: null,
      allData: null
    }
  },
  mounted() {
    this.initChart()
    this.getScatterData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdapter)
  },
  methods: {
    async initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.map_ref, 'chalk')
      const { data: mapData } = await axios.get(
        'http://localhost:8999/static/map/china.json'
      )
      this.$echarts.registerMap('china', mapData)
      const initOption = {
        title: {
          text: '▎ 商家分布',
          left: 20,
          top: 20
        },
        geo: {
          type: 'map',
          map: 'china',
          top: '5%',
          bottom: '5%',
          itemStyle: { areaColor: '#2E72BF', borderColor: '#333' }
        },
        legend: { left: '5%', bottom: '5%', orient: 'vertical' }
      }
      this.chartInstance.setOption(initOption)
      this.chartInstance.on('click', async (arg) => {
        const provinceInfo = getProvinceMapInfo(arg.name)
        const { data: res } = await axios.get(
          'http://localhost:8999' + provinceInfo.path
        )
        this.$echarts.registerMap(provinceInfo.key, res)
        this.chartInstance.setOption({
          geo: {
            map: provinceInfo.key
          }
        })
        // this.updateChart()
        this.getScatterData()
      })
    },
    // 获取散点数据
    async getScatterData() {
      const { data: res } = await this.$http.get('map')
      this.allData = res
      console.log(this.allData)
      this.updateChart()
    },

    updateChart() {
      // 处理图标需要的数据
      //   图例数据

      const legendData = this.allData.map((item) => {
        return item.name
      })
      console.log(legendData)
      //   散点数据
      const seriesArr = this.allData.map((item) => {
        return {
          type: 'effectScatter',
          rippleEffect: {
            scale: 5,
            brushType: 'stroke'
          },
          coordinateSystem: 'geo',
          name: item.name,
          data: item.children
        }
      })
      const dataOption = {
        legend: {
          data: legendData
        },

        series: seriesArr
      }
      this.chartInstance.setOption(dataOption)
    },
    screenAdapter() {
      const titleFontSize = (this.$refs.map_ref.offsetWidth / 100) * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        legend: {
          itemWidth: titleFontSize / 2,
          itemHeight: titleFontSize / 2,
          itemGap: titleFontSize / 2,
          textStyle: {
            fontSize: titleFontSize / 2
          }
        }
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
    revertMap() {
      this.chartInstance.setOption({
        geo: {
          map: 'china'
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
</style>
